/**
 * struct ListNode {
 *  int val;
 *  struct ListNode *next;
 *  ListNode(int x) : val(x), next(nullptr) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head)
    {
        if (!head || !head->next) { return head; }
        ListNode* vhead = new ListNode(-1), * pre = vhead, * p = head;
        pre->next = head;
        while (p)
        {
            if (p->next && p->val == p->next->val)
            {
                while (p->next && p->val == p->next->val)
                {
                    p = p->next;
                }
                pre->next = p->next;
            }
            else
            {
                pre = p;
            }
            p = p->next;
        }
        return vhead->next;
    }
};
